import requests
import time
from bs4 import BeautifulSoup
import re
url = "https://www.umei.cc/bizhitupian/weimeibizhi/"

# 重新封装参数

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"
}
response = requests.get(url=url, headers=headers)
response.encoding = "utf-8"
print(response.text)
main_page = BeautifulSoup(response.text, "html.parser")
alist = main_page.find("div", attrs={'class':'item_list infinite_scroll'}).find_all("a")
for a in alist:
    href = "https://www.umei.cc" + a.get('href')  # 直接通过get就可以拿到属性的值
    # 拿到子页面的源代码
    child_page_resp = requests.get(href)
    child_page_resp.encoding = 'utf-8'
    child_page_text = child_page_resp.text
    # 从子页面中拿到图片的下载路径
    child_page = BeautifulSoup(child_page_text, "html.parser")
    p = child_page.find("div", attrs={"class": "bottom-han1c"})
    
    img = p.find("images")
    # src = img.get("src")
    # 下载图片
    re.compile(r'\bimages: [\n')
    img_resp = requests.get(img)
    # img_resp.content  # 这里拿到的是字节
    img_name = img.split("/")[-1]  # 拿到url中的最后一个/以后的内容
    with open("img/"+img_name, mode="wb") as f:
        f.write(img_resp.content)  # 图片内容写入文件

    print("over!!!", img_name)
    time.sleep(1)

print("all over!!!")



